package org.zxteam.virtualfs;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;
import org.zxteam.virtualfs.org.zxteam.virtualfs.ssh.SshFileSystem;

import java.io.*;
import java.util.Arrays;
import java.util.Properties;

public class App {
    private String filePath;

    static class MyLogger implements com.jcraft.jsch.Logger {
        static java.util.Hashtable name=new java.util.Hashtable();
        static{
            name.put(new Integer(DEBUG), "DEBUG: ");
            name.put(new Integer(INFO), "INFO: ");
            name.put(new Integer(WARN), "WARN: ");
            name.put(new Integer(ERROR), "ERROR: ");
            name.put(new Integer(FATAL), "FATAL: ");
        }
        public boolean isEnabled(int level){
            return true;
        }
        public void log(int level, String message){
            System.err.print(name.get(new Integer(level)));
            System.err.println(message);
        }
    }

    static class MyUserInfo implements UserInfo {
        @Override
        public String getPassphrase() {
            return null;
        }

        @Override
        public String getPassword() {
            return Config.getPassword();
        }

        @Override
        public boolean promptPassword(String s) {
            return true;
        }

        @Override
        public boolean promptPassphrase(String s) {
            return true;
        }

        @Override
        public boolean promptYesNo(String s) {
            return true;
        }

        @Override
        public void showMessage(String s) {
        }
    }

    public static void main(String[] args) throws Exception {

        JSch.setLogger(new MyLogger());

        final JSch jsch = new JSch();


        final Session session = jsch.getSession(Config.getUser(), Config.getHost(), 22);

        // username and password will be given via UserInfo interface.
        session.setUserInfo(new MyUserInfo());

        final Properties prop = new Properties();
        prop.setProperty("StrictHostKeyChecking", "no");
        session.setConfig(prop);

        session.connect();

        final IFileSystem sshFs = new SshFileSystem(session);
        try {
            Iterable<String> varTmpItems = sshFs.list("/home/" + Config.getUser());
            for (String item: varTmpItems) {
                System.out.println(item);
            }

            final byte[] data = new byte[]{65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75};

            {
                final String filePath = "/home/" + Config.getUser() + "/test1.txt";

                // Open output stream to file on remote machine (I want to write new file)
                final OutputStream os = sshFs.openOutputStream(filePath);
                // write 5 ASCII symbols: A B C D E F I J ....
                os.write(data);
                os.close();

                final InputStream is = sshFs.openInputStream(filePath);
                printInputStream(is);

                for (byte b: sshFs.getHashMD5(filePath)) {
                    System.out.print((char)b);
                }
                System.out.println();

                for (byte b: sshFs.getHashSHA1(filePath)) {
                    System.out.print((char)b);
                }
                System.out.println();

                for (byte b: sshFs.getHashSHA512(filePath)) {
                    System.out.print((char)b);
                }
                System.out.println();
            }

            {
                final String filePath = "/home/" + Config.getUser() + "/test2.txt";

                // Open output stream to file on remote machine (I want to write new file)
                final OutputStream os = sshFs.openOutputStream(filePath);

                // write symbols: B C
                os.write(data, 1, 2);

                // write symbols: A B
                os.write(data, 0, 2);

                // write symbols: D
                os.write(data[3]);

                // So, was written: B C A B D
                os.close();

                final InputStream is = sshFs.openInputStream(filePath);
                printInputStream(is);
            }

            Iterable<String> varTmpItemsAfter = sshFs.list("/home/" + Config.getUser());
            for (String item: varTmpItemsAfter) {
                System.out.println(item);
            }

        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            if (sshFs instanceof Closeable) {
                ((Closeable) sshFs).close();
            }
        }

        session.disconnect();
    }

    public static void printInputStream(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);

        }
        System.out.println(sb.toString());
    }
}
